﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace Objects
{
    public class EnemyTank : BaseObject
    {
        SpriteBatch spriteBatch;
        float MovementSpeed = 100;
        public EnemyTank()
        {
            this.TheObjectType = ObjectType.EnemyTank;
            spriteBatch = TopDownShooter.Game1.spriteBatch;
            Image = States.Gameplay.Instance.theTextureManager.GetTexture("TANK");
            Random r = new Random();

            int DirectionToGo = r.Next(0, 4);

            if (DirectionToGo == 0)
            {
                Velocity = new Vector2(0, -MovementSpeed);
                Orientation = 0;

                Position = new Vector2(r.Next(0, 1200), 800);
            }
            if (DirectionToGo == 1)
            {
                Velocity = new Vector2(MovementSpeed, 0);
                Orientation = 90;
                Position = new Vector2(-50, r.Next(0, 800));
            }
            if (DirectionToGo == 2)
            {
                Velocity = new Vector2(0, MovementSpeed);
                Orientation = 180;
                Position = new Vector2(r.Next(0, 1200), -50);
            }
            if (DirectionToGo == 3)
            {
                Velocity = new Vector2(-MovementSpeed, 0);
                Orientation = 270;
                Position = new Vector2(-1320, r.Next(0, 800));
            }

            IsActive = true;

            CollisionRect = new Rectangle((int)Position.X, (int)Position.Y, Image.Width, Image.Height);
        }

        public override void Update(float ElapsedTime)
        {
            

            Position += Velocity * ElapsedTime;
            CollisionRect.X = (int)Position.X;
            CollisionRect.Y = (int)Position.Y;

        }
        public override void Render(float ElapsedTime)
        {
            spriteBatch.Draw(Image, Position, null, Color.Red, MathHelper.ToRadians(Orientation), new Vector2(Image.Width * 0.5f, Image.Height * 0.5f), 1.0f, SpriteEffects.None, 0);
        }

        public override void CheckCollision(BaseObject theObject)
        {
            if (theObject.TheObjectType == ObjectType.Bullet)
            {
                if (CollisionRect.Intersects(theObject.CollisionRect))
                {
                    IsActive = false;
                    States.Gameplay.Instance.AddToScore();
                }
            }
        }
    }
}